from tkinter import *
import random
import openpyxl
from threading import Thread

is_run = False
block = False
global start
start = False
# global count

class MyThread(Thread):
    def __init__(self, name):
        super().__init__()
        self.name = name

    def run(self):  # 固定名字run ！！！必须用固定名
        task()



def insert_point():
     var = input.get() #获取输入的信息
     return var

def randomResult(list,num):
    global is_run
    global block
    global start
    count = 0
    tmp = list
    is_run = True
    start = True


    # if is_run:
    #     window.after(100, randomResult, list, num)


def finalResult():
    global is_run
    global block
    is_run = False
    block = True

def task():
    global block
    list = getPeopleList()
    print("start:",start)
    count = 0
    result = random.sample(list, 3)
    var.set(result)
    while (count <3):
        print("11111111111111")
        # input(f'开始抽{3-count}奖...:')
        result = random.sample(list, 3)
        print("result:",result)
        var.set(result)
        for p in result:
           list.remove(p)  # 删除已中奖者
        count +=1
        while(block):
            continue


def randomRun(list):

    global is_run
    global start

    if is_run:#此部分用于避免多次点击开始造成程序重复执行的错误
        return

    num = insert_point()
    if num:
        start = True
        print("start:-----", start)
        num = int(num)
        randomResult(list,num)
    else:
        is_run = False


def getPeopleList():
    workbook = openpyxl.load_workbook("./peopleList.xlsx", data_only=True)
    list = []
    sheet = workbook[workbook.sheetnames[0]]
    for row in range(2,sheet.max_row+1):
        if sheet.cell(row=row,column=2).value == "":
            continue
        else:
            list.append(sheet.cell(row=row,column=2).value)
    return list


if __name__ == '__main__':
    window = Tk()
    window.geometry('500x290+250+150')
    window.resizable(0,0)
    window.title('抽奖小程序')
    # list = getPeopleList()


    var = StringVar()

    noteLable = Label(text="请输入抽奖人数：")
    noteLable.place(anchor=NW, x=120, y=30)
    input = Entry(window, show=None)
    input.place(anchor=NW, x=240, y=30)

    resultLable = Label(textvariable=var)
    resultLable.place(anchor=NW, x=150, y=100)

    startBt = Button(text="开始", command=task)
    confirmBt = Button(text="确定", command=lambda: finalResult())
    startBt.place(anchor=NW, x=200, y=180)
    confirmBt.place(anchor=NW, x=260, y=180)
    # p = MyThread('子线程1')
    # p.start()



    window.mainloop()
